package com.boco.fznq.network;

import android.text.TextUtils;
import android.util.Log;

import com.boco.fznq.login.bean.BaseBean;
import com.culiu.core.utils.json.JsonUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.io.File;
import java.util.Map;

import okhttp3.Call;

/**
 * Created by jiangcheng on 2017/9/15.
 */

public class HttpUtils {

    public static <T extends BaseBean> void post(String url,
                                                 Map params,
                                                 final Class<T> targeClass,
                                                 final HttpListener<T> listenter) {
        OkHttpUtils.post()
                .url(url)
                .params(params)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        listenter.onError(e);
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        if (!TextUtils.isEmpty(response)) {
                            try {
                                T t = JsonUtils.json2Bean(response, targeClass);
                                listenter.onResponse(t);
                            } catch (Exception e) {
                                Log.d("Json exception" , e.getMessage());
                            }
                        }
                    }
                });
    }

    public static <T extends BaseBean> void upload(String url,
                                                 File file,
                                                 final Class<T> targeClass,
                                                 final HttpListener<T> listenter) {
//        OkHttpUtils.postFile()
//                .url(url)
//                .file(file)
//                .build()
//                .execute();
    }

}
